﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using NWN.GDY.Service.Common;
using NWN.GDY.Service.DataAccess;

namespace NWN.GDY.Service.Core
{
    public class TableBC
    {
        private static volatile List<Room> Tables = null;
        private static object lockObj = new object();

        public static List<Room> GetTablesInstance()
        {
            lock (lockObj)
            {
                if (Tables == null)
                {
                    //Tables = new List<Room>();
                    Tables = new RoomDA().GetRooms(10);
                }
                return Tables;
            }
        }

        public string CreateNewTable(string passport, string nick, string uid, string tablename)
        {
            string sOut = "";
            //sOut = System.Guid.NewGuid().ToString();
            //Room tb = new Room(sOut, passport, nick, uid, tablename);
            //GetTablesInstance().Add(tb);
            return sOut;
        }

        public List<Room> GetALLTables()
        {
            return GetTablesInstance();
        }

        public Room GetRoom(string roomGUID)
        {
            foreach (Room rm in GetTablesInstance())
            {
                if (rm.RoomGUID == roomGUID)
                    return rm;
            }
            return null;
        }

        public void FinishTable(string guid)
        {
            lock (lockObj)
            {
                List<Room> tables = GetTablesInstance();
                for (int i = 0; i < tables.Count; i++)
                {
                    if (tables[i].RoomGUID == guid)
                    {
                        tables.RemoveAt(i);
                        return;
                    }
                }
            }
        }

        public void FinishAllTable()
        {
            lock (lockObj)
            {
                Tables = null;
            }
        }

        public int OcuppySeat(string tableGUID, Player player)
        {
            return 0;
            //Room tb = GetRoom(tableGUID);
            //lock (lockObj)
            //{
            //    if (tb == null)
            //        return GDYConsts.ReturnDefine.NOTABLE;
            //    if (tb.Seats[player.SitSeat].OccupiedPlayer == null)
            //    {
            //        //有相同UID或昵称的不能进
            //        foreach (Player pl in tb.Players)
            //        {
            //            if (pl.UID == player.UID || string.Compare(pl.NickName, player.NickName, true) == 0)
            //                return GDYConsts.ReturnDefine.NickAlreadyInTable;
            //        }
            //        //加人员信息
            //        player.Ready = false;
            //        tb.Players.Add(player);
            //        //处理坐位信息
            //        tb.Seats[player.SitSeat].OccupiedPlayer = player;
            //        return GDYConsts.ReturnDefine.SUCCESS;
            //    }
            //    else
            //        return GDYConsts.ReturnDefine.SeatIsOccupied;
            //}
        }

        public int ToggleSeatDisable(string tableGUID, int seatID, string commanduid, out bool seatDisabled)
        {
            seatDisabled = false;
            return 0;
            //seatDisabled = false;
            //Room tb = GetRoom(tableGUID);
            //lock (lockObj)
            //{
            //    if (tb == null)
            //        return GDYConsts.ReturnDefine.NOTABLE;
            //    //未被占才能改状态
            //    if (tb.Seats[seatID].OccupiedPlayer != null)
            //        return GDYConsts.ReturnDefine.SeatIsOccupied;
            //    //创建者才能改状态
            //    if (tb.Creator.UID != commanduid)
            //        return GDYConsts.ReturnDefine.NotCreator;
            //    tb.Seats[seatID].Disabled = !tb.Seats[seatID].Disabled;
            //    seatDisabled = tb.Seats[seatID].Disabled;
            //    return GDYConsts.ReturnDefine.SUCCESS;
            //}
        }

        public int PlayerReady(string tableGUID, string playerID)
        {
            //Room tb = GetRoom(tableGUID);
            //foreach (Player pl in tb.Players)
            //{
            //    if (pl.UID == playerID)
            //    {
            //        pl.Ready = true;
            //        return GDYConsts.ReturnDefine.SUCCESS;
            //    }
            //}
            return GDYConsts.ReturnDefine.NoPlayer;
        }
    }
}
